package com.lingtianyu.tech.erbaoshen.core.consumer.openfeign;

import feign.RequestInterceptor;
import feign.Target;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class ErBaoShenOpenFeignConfig {
    public static final String SERVER_NAME_KEY = "LingTianYu-ErBaoShen-Server-Name";

    @Bean
    public RequestInterceptor cloudContextRequestInterceptor() {
        return requestTemplate -> {
            Target<?> target = requestTemplate.feignTarget();
            if(target.type().equals(OpenFeignErBaoShenClient.class) && ErBaoShenOpenFeignContext.getServerName() != null && !ErBaoShenOpenFeignContext.getServerName().isEmpty()) {
                Target.HardCodedTarget hardCodedTarget = new Target.HardCodedTarget(OpenFeignErBaoShenClient.class, ErBaoShenOpenFeignContext.getServerName(), "http://" + ErBaoShenOpenFeignContext.getServerName());
                requestTemplate.feignTarget(hardCodedTarget);
                requestTemplate.target("http://" + ErBaoShenOpenFeignContext.getServerName());
            }
        };
    }
}
